package com.icim.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.icim.entity.Ret_tm_cus_prd_in;
import com.icim.util.TimeUtil;
import com.icim.xmtm.entity.CtSlimifshipbox;

@Repository
public class RetTmCusPrdInDao {
	@Resource
	private JdbcTemplate icimJdbcTemplate;

	public void save(Ret_tm_cus_prd_in retTmCusPrdIn) {

		StringBuffer sql = new StringBuffer("INSERT INTO RET_TM_CUS_PRD_IN");
		sql.append("(");
		sql.append("FACTORYNAME,PRODUCTREQUESTNAME,PRODUCTSPECNAME,PRODUCTTYPE,THICKNESS,");
		sql.append("TARGETPRODUCTSPECNAME,TARGETPRODUCTREQUEST,TARGETTHICKNESS,LOTNAME,BOXNAME,");
		sql.append("BOXTYPE,BOXCAPACITY,PRODUCTQUANTITY,EVENTUSER, EVENTTIME,");
		sql.append("RELEASEQUANTITY,RECEIVESTATE,RECEIVEFLAG, DELIVERYTIME,ERRORMESSAGE");
		sql.append(")");
		sql.append("VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		icimJdbcTemplate.update(sql.toString(), retTmCusPrdIn.getFactoryname(),
				retTmCusPrdIn.getProductrequestname(),
				retTmCusPrdIn.getProductspecname(),
				retTmCusPrdIn.getProducttype(), retTmCusPrdIn.getThickness(),
				retTmCusPrdIn.getTargetproductspecname(),
				retTmCusPrdIn.getTargetproductrequest(),
				retTmCusPrdIn.getTargetthickness(), retTmCusPrdIn.getLotname(),
				retTmCusPrdIn.getBoxname(), retTmCusPrdIn.getBoxtype(),
				retTmCusPrdIn.getBoxcapacity(),
				retTmCusPrdIn.getProductquantity(),
				retTmCusPrdIn.getEventuser(), retTmCusPrdIn.getEventtime(),
				retTmCusPrdIn.getReleasequantity(),
				retTmCusPrdIn.getReceivestate(),
				retTmCusPrdIn.getReceiveflag(), TimeUtil.getCurrentTimestamp(),
				retTmCusPrdIn.getErrormessage());

	}

	 private Ret_tm_cus_prd_in setEntityByRs(ResultSet rs) throws SQLException {
         Ret_tm_cus_prd_in retTmCusPrdIn = new Ret_tm_cus_prd_in();
         retTmCusPrdIn.setBoxname(rs.getString("BOXNAME"));
         retTmCusPrdIn.setProducttype(rs
                 .getString("PRODUCTTYPE"));
         retTmCusPrdIn.setBoxtype(rs.getString("BOXTYPE"));
         retTmCusPrdIn.setLotname(rs.getString("LOTNAME"));
         retTmCusPrdIn.setProductspecname(rs.getString("PRODUCTSPECNAME"));
         retTmCusPrdIn.setTargetproductspecname(rs.getString("TARGETPRODUCTSPECNAME"));
         return retTmCusPrdIn;
     }
	 
	public Ret_tm_cus_prd_in get(String boxId) {
		String sql = "SELECT * FROM RET_TM_CUS_PRD_IN WHERE BOXNAME = ? ";

		List<Ret_tm_cus_prd_in> retTmCusPrdInList = icimJdbcTemplate.query(sql,
				new Object[] { boxId }, new RowMapper<Ret_tm_cus_prd_in>() {
					@Override
					public Ret_tm_cus_prd_in mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return setEntityByRs(rs);
					}

				});
		return retTmCusPrdInList.isEmpty() ? null : retTmCusPrdInList.get(0);

	}

	public void update(Ret_tm_cus_prd_in box) {
		StringBuffer sql = new StringBuffer("UPDATE RET_TM_CUS_PRD_IN SET ");
		sql.append("FACTORYNAME=?,PRODUCTREQUESTNAME=?,PRODUCTSPECNAME=?,");
		sql.append("PRODUCTTYPE=?,THICKNESS=?,TARGETPRODUCTSPECNAME=?,");
		sql.append("TARGETPRODUCTREQUEST=?,TARGETTHICKNESS=?,LOTNAME=?,");
		sql.append("BOXTYPE=?,BOXCAPACITY=?,PRODUCTQUANTITY=?,");
		sql.append("EVENTUSER=?,EVENTTIME=?,RELEASEQUANTITY=?,");
		sql.append("RECEIVESTATE=?,RECEIVEFLAG=?,DELIVERYTIME=?,ERRORMESSAGE=?");
		sql.append("WHERE BOXNAME = ?");
		icimJdbcTemplate.update(
				sql.toString(),
				new Object[] { box.getFactoryname(),
						box.getProductrequestname(), box.getProductspecname(),
						box.getProducttype(), box.getThickness(),
						box.getTargetproductspecname(),
						box.getTargetproductrequest(),
						box.getTargetthickness(), box.getLotname(),
						box.getBoxtype(), box.getBoxcapacity(),
						box.getProductquantity(), box.getEventuser(),
						box.getEventtime(), box.getReleasequantity(),
						box.getReceivestate(), box.getReceiveflag(),
						box.getDeliverytime(), box.getErrormessage(),
						box.getBoxname() });
	}
}
